package lwh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import lwh.utils.DBOpenHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ContractService {
	private DBOpenHelper dbOpenHelper;
	public ContractService(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}
	
	public List<HashMap<String,Object>> getInitListByUserName(String userName){
		List<HashMap<String,Object>> data =new ArrayList<HashMap<String,Object>>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT d.prj_name,d.prj_desc,e.prj_dev_name,d.prj_company,d.prj_id FROM sys_user_account a,sys_user_authcode b,sys_authcode_prj_contract c,prj_contract d,prj_develop e WHERE a.user_account=? AND a.user_id=b.user_id AND b.authcode_id=c.authcode_id AND c.prj_id=d.prj_id AND d.prj_dev_id=e.prj_dev_id",
				new String[]{userName});
		while(cursor.moveToNext()){
			HashMap<String,Object> item=new HashMap<String, Object>();
			item.put("prj_id", cursor.getString(cursor.getColumnIndex("prj_id")));
    		item.put("prj_name", cursor.getString(cursor.getColumnIndex("prj_name")));
    		item.put("prj_desc", cursor.getString(cursor.getColumnIndex("prj_desc")));
    		item.put("prj_dev_name", cursor.getString(cursor.getColumnIndex("prj_dev_name")));
    		item.put("prj_company", cursor.getString(cursor.getColumnIndex("prj_company")));
    		data.add(item);
		}
		cursor.close();
		db.close();
		return data;
	}
	
	public List<HashMap<String,Object>> getListByUserNameAndCName(String userName,String contractName){
		List<HashMap<String,Object>> data =new ArrayList<HashMap<String,Object>>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("SELECT d.prj_name,d.prj_desc,e.prj_dev_name,d.prj_company,d.prj_id FROM sys_user_account a,sys_user_authcode b,sys_authcode_prj_contract c,prj_contract d,prj_develop e WHERE a.user_account=? AND d.prj_name like ?  AND a.user_id=b.user_id AND b.authcode_id=c.authcode_id AND c.prj_id=d.prj_id AND d.prj_dev_id=e.prj_dev_id",
				new String[]{userName,"%"+contractName+"%"});
		while(cursor.moveToNext()){
			HashMap<String,Object> item=new HashMap<String, Object>();
			item.put("prj_id", cursor.getString(cursor.getColumnIndex("prj_id")));
    		item.put("prj_name", cursor.getString(cursor.getColumnIndex("prj_name")));
    		item.put("prj_desc", cursor.getString(cursor.getColumnIndex("prj_desc")));
    		item.put("prj_dev_name", cursor.getString(cursor.getColumnIndex("prj_dev_name")));
    		item.put("prj_company", cursor.getString(cursor.getColumnIndex("prj_company")));
    		data.add(item);
		}
		cursor.close();
		db.close();
		return data;
	}
	public void closeDBHelper(){
		dbOpenHelper.close();
	}
}
